package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.IdDTO;
import com.guoheng.safe.sys.dto.basicinfo.ReservoirDataAddDTO;
import com.guoheng.safe.sys.dto.basicinfo.ReservoirDataPageDTO;
import com.guoheng.safe.sys.dto.basicinfo.ReservoirDataUpdateDTO;
import com.guoheng.safe.sys.service.ReservoirDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.model.basicinfo.ReservoirData;
import com.guoheng.safe.sys.mapper.basicinfo.ReservoirDataMapper;

import java.util.List;
/**
 * 类功能描述:　ReservoirDataServiceImpl
 *
 * @author Eternal
 * @date 2019-20-24 9:20
 */
@Service
public class ReservoirDataServiceImpl implements ReservoirDataService {

    @Resource
    private ReservoirDataMapper reservoirDataMapper;

    @Override
    public Result addReservoirData(ReservoirDataAddDTO reservoirDataAddDTO) {
        ReservoirData reservoirData = new ReservoirData();
        BeanUtils.copyProperties(reservoirDataAddDTO,reservoirData);
        int result = reservoirDataMapper.insert(reservoirData);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateReservoirData(ReservoirDataUpdateDTO reservoirDataUpdateDTO) {
        ReservoirData reservoirData = new ReservoirData();
        BeanUtils.copyProperties(reservoirDataUpdateDTO,reservoirData);
        int result = reservoirDataMapper.updateByPrimaryKeySelective(reservoirData);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result deleteReservoirData(IdDTO idDTO) {
        int result = reservoirDataMapper.deleteByPrimaryKey(idDTO.getId());
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getReservoirData(IdDTO idDTO) {
        ReservoirData reservoirData = reservoirDataMapper.selectByPrimaryKey(idDTO.getId());
        if (null == reservoirData){
            return Result.failure(ErrorCodeEnum.SYS_ERR_SEARCH_FAILED);
        }
        return Result.success(reservoirData);
    }

    @Override
    public Result getReservoirDataPage(ReservoirDataPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        List<ReservoirData> reservoirData = reservoirDataMapper.selectAll();
        PageInfo<ReservoirData> pageInfo = new PageInfo<>(reservoirData);
        return Result.success(pageInfo);
    }

}

